<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分布式Session共享解决方案</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        /* 首字下沉效果 */
        .drop-cap::first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #4f46e5;
            font-family: 'Noto Serif SC', serif;
        }
        
        /* 代码块美化 */
        pre {
            background: #1e293b;
            color: #e2e8f0;
            border-radius: 12px;
            padding: 1.5rem;
            overflow-x: auto;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            position: relative;
        }
        
        pre::before {
            content: attr(data-language);
            position: absolute;
            top: 0.5rem;
            right: 1rem;
            font-size: 0.75rem;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }
        
        /* 引用块样式 */
        .quote-block {
            position: relative;
            padding-left: 2rem;
            border-left: 4px solid #4f46e5;
            background: linear-gradient(to right, rgba(79, 70, 229, 0.05), transparent);
            margin: 2rem 0;
            padding: 1.5rem 2rem;
            border-radius: 0 12px 12px 0;
        }
        
        .quote-block::before {
            content: '\f10d';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            left: -0.5rem;
            top: -0.5rem;
            font-size: 2rem;
            color: #4f46e5;
            opacity: 0.3;
        }
        
        /* 卡片悬停效果 */
        .solution-card {
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .solution-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        /* 标题装饰 */
        .section-title {
            position: relative;
            display: inline-block;
            padding-bottom: 0.5rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 3px;
            background: linear-gradient(to right, #4f46e5, #7c3aed);
            border-radius: 2px;
        }
        
        /* 图片样式 */
        img {
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 2rem auto;
            display: block;
            max-width: 100%;
        }
        
        /* 代码内联样式 */
        code:not(pre code) {
            background: #e0e7ff;
            color: #4f46e5;
            padding: 0.2rem 0.5rem;
            border-radius: 4px;
            font-size: 0.9em;
            font-weight: 500;
        }
        
        /* 滚动动画 */
        .fade-in {
            opacity: 0;
            transform: translateY(20px);
            animation: fadeIn 0.8s ease forwards;
        }
        
        @keyframes fadeIn {
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* Mermaid图表样式 */
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="relative overflow-hidden bg-gradient-to-br from-indigo-600 via-purple-600 to-pink-500 text-white">
        <div class="absolute inset-0 bg-black opacity-20"></div>
        <div class="relative container mx-auto px-6 py-24">
            <div class="max-w-4xl mx-auto text-center fade-in">
                <h1 class="text-5xl md:text-7xl font-bold mb-6 leading-tight">
                    分布式Session
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">共享解决方案</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    深入探索微服务架构下的会话管理策略，构建高可用的分布式系统
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-server mr-2"></i> 集群部署
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-sync-alt mr-2"></i> 会话同步
                    </span>
                    <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                        <i class="fas fa-shield-alt mr-2"></i> 高可用性
                    </span>
                </div>
            </div>
        </div>
        <div class="absolute bottom-0 left-0 right-0">
            <svg viewBox="0 0 1440 120" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 120L60 105C120 90 240 60 360 45C480 30 600 30 720 37.5C840 45 960 60 1080 67.5C1200 75 1320 75 1380 75L1440 75V120H1380C1320 120 1200 120 1080 120C960 120 840 120 720 120C600 120 480 120 360 120C240 120 120 120 60 120H0Z" fill="white"/>
            </svg>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12 max-w-6xl">
        
        <!-- 问题引入 -->
        <section class="mb-16 fade-in">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <h2 class="text-3xl font-bold mb-6 text-gray-800">
                    <span class="section-title">Session共享问题</span>
                </h2>
                <div class="quote-block">
                    <p class="text-gray-700 leading-relaxed drop-cap">
                        HttpSession是通过Servlet容器创建和管理的，像Tomcat/Jetty都是保存在内存中的。而如果我们把web服务器搭建成分布式的集群，然后利用LVS或Nginx做负载均衡，那么来自同一用户的Http请求将有可能被分发到两个不同的web站点中去。那么问题就来了，如何保证不同的web站点能够共享同一份session数据呢？
                    </p>
                </div>
            </div>
        </section>

        <!-- 解决方案概览 -->
        <section class="mb-16 fade-in">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 text-center">
                <span class="section-title">Session共享解决方案</span>
            </h2>
            
            <!-- 方案卡片网格 -->
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <!-- Session复制 -->
                <div class="solution-card bg-white rounded-2xl shadow-lg p-8">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-red-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-copy text-red-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">Session复制</h3>
                    </div>
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651305084153-f18da983-48f2-42a3-8508-b107d128e958.png" alt="Session复制架构图" class="w-full mb-4">
                    <div class="space-y-4">
                        <div class="bg-green-50 rounded-lg p-4">
                            <h4 class="font-semibold text-green-800 mb-2"><i class="fas fa-check-circle mr-2"></i>优点</h4>
                            <p class="text-gray-700">web-server (Tomcat) <code>原生支持</code>，只需要修改配置文件</p>
                        </div>
                        <div class="bg-red-50 rounded-lg p-4">
                            <h4 class="font-semibold text-red-800 mb-2"><i class="fas fa-times-circle mr-2"></i>缺点</h4>
                            <ul class="text-gray-700 space-y-2">
                                <li>• session同步需要数据传输，占用大量网络带宽，降低了服务器群的业务处理能力</li>
                                <li>• 任意一台web-server保存的数据都是所有web-server的session总和，受到内存限制无法水平扩展更多的web-server</li>
                                <li>• 大型分布式集群情况下，由于所有web-server都全量保存数据，所以此方案不可取</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- 客户端存储 -->
                <div class="solution-card bg-white rounded-2xl shadow-lg p-8">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-yellow-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-cookie-bite text-yellow-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">客户端存储</h3>
                    </div>
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651305095875-f0c99271-a573-487e-8337-3b1075ac1801.png" alt="客户端存储架构图" class="w-full mb-4">
                    <div class="space-y-4">
                        <div class="bg-green-50 rounded-lg p-4">
                            <h4 class="font-semibold text-green-800 mb-2"><i class="fas fa-check-circle mr-2"></i>优点</h4>
                            <p class="text-gray-700">服务器不需存储session，用户保存自己的session信息到cookie中，节省服务端资源</p>
                        </div>
                        <div class="bg-red-50 rounded-lg p-4">
                            <h4 class="font-semibold text-red-800 mb-2"><i class="fas fa-times-circle mr-2"></i>缺点</h4>
                            <p class="font-semibold text-red-700 mb-2">这只是一种思路，不会使用该方式，具体原因如下：</p>
                            <ul class="text-gray-700 space-y-2">
                                <li>• 每次http请求，携带用户在cookie中的完整信息，浪费网络带宽</li>
                                <li>• session数据放在cookie中，cookie有长度限制4K，不能保存大量信息</li>
                                <li>• session数据放在cookie中，存在泄漏、篡改、窃取等安全隐患</li>
                            </ul>
                        </div>
                    </div>
                </div>

                <!-- Hash一致性 -->
                <div class="solution-card bg-white rounded-2xl shadow-lg p-8">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-hashtag text-blue-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">Hash一致性</h3>
                    </div>
                    <img src="https://cdn.nlark.com/yuque/0/2022/png/21449790/1651305103755-f25373a7-6286-4da4-b216-268d8b1c84e7.png" alt="Hash一致性架构图" class="